<template>
  <div class="app-container">
    <div>
      <el-row :gutter="10">
        <el-col :span="6" :xs="24" :lg="24">
          <div class="bgwhite plr20 ptb20 radius10 flex-col">
            <div class="justify-between w100p">
              <div class="clr3c4 bold ft26">編輯</div>
              <div class="clr9fa align-center pointer" @click="back()">
                <el-icon>
                  <Back />
                </el-icon>
                返回上一頁
              </div>
            </div>
            <div class="mt40">
              <el-form :label-position="labelPosition" label-width="auto" :model="formLabelAlign.user" style="max-width: 600px">
                <el-form-item label="用戶ID:">
                  <el-input v-model="formLabelAlign.user.id" disabled style="width: 380px;" placeholder="請輸入用戶名" />
                </el-form-item>
                <el-form-item label="賬號:">
                  <el-input v-model="formLabelAlign.user.phone" disabled style="width: 380px;" placeholder="請輸入用戶名" />
                </el-form-item>
                <el-form-item label="昵稱:">
                  <el-input v-model="formLabelAlign.user.name" style="width: 380px;" placeholder="請輸入用戶名" />
                </el-form-item>
                <el-form-item label="頭像:" style="align-items:flex-start;font-weight:bold;">
                  <el-upload v-model:file-list="fileList" :action="$upUrl" list-type="picture-card" :headers="headers" name="images" :on-success="onSuccess" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
                    <div class="uploader flex-cen" v-if="fileList.length == 0">
                      <el-icon size="60" style="color: #3a3f63;">
                        <Picture />
                      </el-icon>
                    </div>
                    <el-button class="btn2 ml20">選擇文件</el-button>
                  </el-upload>
                </el-form-item>
                <el-form-item label="餘額:">
                  <el-input v-model="formLabelAlign.user.coin" disabled style="width: 380px;" placeholder="請輸入餘額" />
                </el-form-item>
                <el-form-item label="手機號:">
                  <el-input v-model="formLabelAlign.user.phone" disabled style="width: 380px;" placeholder="請輸入手機號" />
                </el-form-item>
                <el-form-item label="郵箱:">
                  <el-input v-model="formLabelAlign.user.email" style="width: 380px;" placeholder="請輸入郵箱" />
                </el-form-item>
              </el-form>
              <div class="mt40 mb15 flex-cen ">
                <div class="align-center">
                  <el-button class="btn1" @click="reset()">重置</el-button>
                  <el-button class="btn2" @click="submit()">提交</el-button>
                </div>

              </div>
            </div>


          </div>
        </el-col>

      </el-row>
    </div>

  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router';
import type { FormProps } from 'element-plus'
import { baseURL, headers, formateDate } from '@/utils/utils'
import { userEdit } from '@/api/usersManage'
const route = useRoute()
onMounted(() => {
  formLabelAlign.user = route.query
  console.log(formLabelAlign.user, route.query.row)
  let file = [
    {
      name: 'avatar',
      url: route.query.avatar
    }
  ]
  fileList.value = file
})
const onSuccess = (res) => {
  console.log('上传成功', res)
  let path = res.data[0].path
  let fullpath = `${baseURL}${path}`
  let file = [
    {
      name: 'avatar',
      url: fullpath
    }
  ]
  fileList.value = file

}

const formLabelAlign = reactive({
  user: {}
})
const labelPosition = ref<FormProps['labelPosition']>('right')

const fileList = ref([])

const dialogImageUrl = ref('')

const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
  fileList.value = []
}

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}
const submit = () => {
  
  if(fileList.value.length == 0){
    return ElMessage.error('請上傳頭像')
  }
  userEdit({
    id: formLabelAlign.user.id,
    phone: formLabelAlign.user.phone,
    name: formLabelAlign.user.name,
    avatar: fileList.value.length ? fileList.value[0].url : '',
    email: formLabelAlign.user.email
  }).then((res) => {
    console.log('編輯成功', res)
    back()
  })
}
const reset = () => {
  // formLabelAlign.user.phone = ''
  formLabelAlign.user.name = ''
  formLabelAlign.user.email = ''
  fileList.value = []
}

const router = useRouter()
const back = () => {
  router.back()
}
</script>
<style scoped >
:deep(.el-upload--picture-card) {
  background-color: transparent;
  border: none;
  width: auto
}

:deep(.el-input) {
  box-shadow: none;
}

:deep(.el-input) {
  --el-input-focus-border-color: #dcdfe6;
  --el-input-hover-border-color: #dcdfe6;
  --el-input-focus-border: transparent;
}
</style>